package com.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.common.QueryPageParam;
import com.wms.common.Result;
import com.wms.entity.Menu;
import com.wms.entity.Source;
import com.wms.entity.Storage;
import com.wms.service.SourceService;
import com.wms.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
/*
@RestController
@RequestMapping("/source")
public class SourceController {

    private final SourceService sourceService;

    @Autowired
    public SourceController(SourceService sourceService) {
        this.sourceService = sourceService;
    }

    @GetMapping("/{id}")
    public Source getSourceById(@PathVariable Integer id) {
        return sourceService.getById(id);
    }

    @PostMapping("/")
    public boolean createSource(@RequestBody Source source) {
        return sourceService.save(source);
    }

    @PutMapping("/{id}")
    public boolean updateSource(@PathVariable Integer id, @RequestBody Source source) {
        source.setId(id);
        return sourceService.updateById(source);
    }

    // Add other methods as needed...
}
*/
@RestController
@RequestMapping("/source")
public class SourceController {

    @Autowired
    private SourceService sourceService;

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Source source){
        return sourceService.save(source)?Result.suc():Result.fail();
    }

    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Source source){
        return sourceService.updateById(source)?Result.suc():Result.fail();
    }

    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return sourceService.removeById(id)?Result.suc():Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String sourceName = (String)param.get("sourceName");

        Page<Source> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Source> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(sourceName) && !"null".equals(sourceName)){
            lambdaQueryWrapper.like(Source::getSourceName,sourceName);
        }

        IPage result = sourceService.pageCC(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }

    @GetMapping("/list")
    public Result list(){
        List list = sourceService.list();
        return Result.suc(list);
    }
}
